package com.gtt.lazy.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.gtt.lazy.domain.Sofa;
import com.gtt.lazy.service.SofaService;

/**
 * 沙发管理Action
 * 
 * @author 高甜甜
 * @date 2009-7-9
 */
public class SofaAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private SofaService sofaService;

	private List<Sofa> results;
	private Sofa sofa;

	private int id;
	private String name;
	private String cloth;
	private String size;
	private double weight;
	private String packing;
	private String memo;
	private String number;
	private double prize;
	private int recommend;
	private String yunfei;

	private File upload;
	private String contentType;
	private String fileName;
	private InputStream imageStream;

	public String list() {
		// 确定所要显示的页数
		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE;
		results = sofaService.list(start, Constant.SIZE);
		setPagination(sofaService.count(), Constant.SIZE, page);
		return SUCCESS;
	}

	public String toAdd() {
		return SUCCESS;
	}

	public String add() {
		Sofa sofa = new Sofa(name);
		sofa.setSize(size);
		sofa.setWeight(weight);
		sofa.setPacking(packing);
		sofa.setCloth(cloth);
		sofa.setMemo(memo);
		sofa.setNumber(number);
		sofa.setPrize(prize);
		sofa.setRecommend(recommend);
		sofa.setYunfei(yunfei);
		int sid = sofaService.add(sofa);

		if (upload != null) {
			String image = ImageUtil.store(sid, fileName, upload,
					getImagePath());
			sofa.setId(sid);
			sofa.setImage(image);
			sofaService.update(sofa);
		}
		return SUCCESS;
	}

	public String toMod() {
		sofa = sofaService.get(id);
		return SUCCESS;
	}

	public String mod() {

		Sofa sofa = sofaService.get(id);
		sofa.setName(name);
		sofa.setSize(size);
		sofa.setWeight(weight);
		sofa.setPacking(packing);
		sofa.setCloth(cloth);
		sofa.setMemo(memo);
		sofa.setNumber(number);
		sofa.setPrize(prize);
		sofa.setRecommend(recommend);
		sofa.setYunfei(yunfei);

		if (upload != null) {
			String image = sofa.getImage();
			try {
				ImageUtil.delete(getImagePath(), image);
			} catch (IOException e) {
			}
			image = ImageUtil.store(sofa.getId(), fileName, upload,
					getImagePath());
			sofa.setImage(image);
		}
		sofaService.update(sofa);
		return SUCCESS;
	}

	public String del() {
		String image = sofaService.get(id).getImage();
		sofaService.delete(id);
		try {
			if (!StringUtils.isEmpty(image))
				ImageUtil.move(getImagePath(), image, getImageBakPath());
		} catch (IOException e) {
		}
		return SUCCESS;
	}

	public String getAttach() {
		Sofa sofa = sofaService.get(id);
		this.fileName = sofa.getImage();
		imageStream = ImageUtil.get(fileName, getImagePath());
		if (imageStream == null) {
			this.fileName = Constant.IMG_ERROR;
			imageStream = ImageUtil
					.get(Constant.IMG_ERROR, getImageErrorPath());
		}
		this.contentType = getMimeType(fileName);
		return SUCCESS;
	}

	/**
	 * 获取图片存储的根路径
	 * 
	 * @return
	 */
	private String getImagePath() {
		return ServletActionContext.getServletContext().getRealPath(
				File.separator + "upload");
	}

	/**
	 * 获取图片备份目录
	 * 
	 * @return
	 */
	private String getImageBakPath() {
		return ServletActionContext.getServletContext().getRealPath(
				File.separator + "bak");
	}

	public String getFileName() {
		return fileName;
	}

	public String getContentType() {
		return contentType;
	}

	public InputStream getImageStream() {
		return imageStream;
	}

	public String makeDiscount() {
		sofaService.updateDiscount(id, true);
		return SUCCESS;
	}

	public String makeFresh() {
		sofaService.updateFresh(id, true);
		return SUCCESS;
	}

	public String makeunDiscount() {
		sofaService.updateDiscount(id, false);
		return SUCCESS;
	}

	public String makeunFresh() {
		sofaService.updateFresh(id, false);
		return SUCCESS;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCloth() {
		return cloth;
	}

	public void setCloth(String cloth) {
		this.cloth = cloth;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public String getPacking() {
		return packing;
	}

	public void setPacking(String packing) {
		this.packing = packing;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public double getPrize() {
		return prize;
	}

	public void setPrize(double prize) {
		this.prize = prize;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Sofa getSofa() {
		return sofa;
	}

	public void setSofa(Sofa sofa) {
		this.sofa = sofa;
	}

	public List<Sofa> getResults() {
		return results;
	}

	public void setResults(List<Sofa> results) {
		this.results = results;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public int getRecommend() {
		return recommend;
	}

	public void setRecommend(int recommend) {
		this.recommend = recommend;
	}

	public String getYunfei() {
		return yunfei;
	}

	public void setYunfei(String yunfei) {
		this.yunfei = yunfei;
	}

	public SofaService getSofaService() {
		return sofaService;
	}

	public void setSofaService(SofaService sofaService) {
		this.sofaService = sofaService;
	}

}
